package com.gem.demo.controller;

import com.gem.demo.entity.Product;
import com.gem.demo.entity.ProductType;
import com.gem.demo.service.ProductService;
import com.gem.demo.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.List;

@Controller
@RequestMapping("/product")
@SessionAttributes({"types","products"})
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductTypeService productTypeService;
    @RequestMapping("/index")
    public String index(Model model){
        //所有分类
        List<ProductType>types=productTypeService.list();
        //第一分类的商品
        int typeid=types.get(0).getId();
        List<Product>products=productService.list(typeid);
        model.addAttribute("types",types);
        model.addAttribute("products",products);
        return "product/index";
    }
    //加载商品展示主页
    @RequestMapping("/mainPage")
    public String mainPage(){
        return "product/main";
    }
    //分类请求
    @RequestMapping("/getProduct")
    public String getProduct(int typeid,Model model){
        System.out.println(typeid);
        List<Product>products=productService.list(typeid);
        model.addAttribute("products",products);
        return "product/main";
    }
}
